package com.cy.law.utils;


import org.apache.commons.codec.binary.Base64;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;


public class StringUtil {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public static String replaceQuotes(String str) {
        if (str == null)
            return "";
        return str.replaceAll("\'", "''");
    }

    public static String MD5Encode(String origin) {
        String resultString = null;
        resultString = origin;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
        } catch (Exception ex) {
            // no deal
        }
        return resultString.toUpperCase();
    }

    public static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String getFileExt(String fileName) {
        if (fileName != null) {
            int i = fileName.lastIndexOf('.');
            if (i != -1) {
                return fileName.substring(i + 1);
            }
        }
        return "";
    }

    public static String nullToStr(String str) {
        if (str == null)
            return "";
        else
            return str;
    }

    public static String getContent(String strUrl) throws UnsupportedEncodingException, IOException {
        URL url = new URL(strUrl);
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
        String s = "";
        StringBuffer sb = new StringBuffer("");
        while ((s = br.readLine()) != null) {
            sb.append(s + "\r\n");
        }
        br.close();
        return sb.toString();
    }

    public static String strToASCIIWithSpliter(String str, String spliter) {
        if (str == null) {
            return null;
        }
        char[] chars = str.toCharArray();
        String strCode = "";
        for (int i = 0; i < chars.length; i++) {//输出结果
            if (i == 0) {
                strCode += "" + (int) chars[i];
            } else {
                strCode += spliter + (int) chars[i];
            }
        }
        return strCode;
    }

    public static String ASCIIWithSpliterToStr(String strCode, String regex) {
        if (strCode == null) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        String[] ch = strCode.split(regex);
        for (int i = 0; i < ch.length; i++) {
            str.append((char) Integer.parseInt(ch[i]));
        }
        return str.toString();
    }

    public static String base64Decode(final String str) throws UnsupportedEncodingException {
        byte[] tmp = base64Decode(str.getBytes(StandardCharsets.UTF_8));
        return new String(tmp, StandardCharsets.UTF_8);
    }

    private static byte[] base64Decode(final byte[] bytes) {
        return Base64.decodeBase64(bytes);
    }

    public static String base64Encode(final byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }

    public static String generateHumanCodeByHumanID(int humanId) {
        String str = humanId + "";
        int length = str.length();
        for (int i = 0; i < 6 - length; i++) {
            str = "0" + str;
        }
        return str;
    }

}
